/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.message;

import com.apt.bean.User;
import com.apt.tag.GetUser;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bach
 */
public class SendMailAutomatic {
    // tim nhung tai khoan het han

    private static HashMap<Integer, User> getAccountExpired() {
        HashMap<Integer, User> hm = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            Date d = new Date(System.currentTimeMillis());
            String date = sdf.format(d);
            String query = "select * from Accounts where Date_Expired= '2008/08/08'";// + date;
            hm = GetUser.getListUser(query);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return hm;
    }

//    /    Truyền tham số tưong ứng
//smtpServer: nếu sử dụng gmail địa chỉ là : smtp.gmail.com
//to : email cần gửi
//from : email của bạn trên gmail
//psw : password của bạn trên gmail
//subject : tiêu đề
//body : nội dung
    public static void sendMail_toAccExpired() {
        HashMap<Integer, User> hm = getAccountExpired();
        Properties p = new Properties();
        try {

            p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
            String fromEmail = p.getProperty("fromEmail");
            String passEmail = p.getProperty("passEmail");
            for (Integer key : hm.keySet()) {
                User u = hm.get(key);
//                String emailto = u.getUser_Email();
                String emailto = "muahoa_nang@yahoo.com";
                Email.send("smtp.gmail.com", emailto, fromEmail, passEmail, "thu nghiem gui mail", "noi dung thu nghiem");
            }
        } catch (IOException ex) {
            Logger.getLogger(SendMailAutomatic.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(SendMailAutomatic.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}